# -*- coding: utf-8 -*-
import scrapy
import os
import sys
import traceback
from productinfo.items import ProductinfoItem

basedir = os.path.dirname(os.path.dirname(__file__))

def get_products():
    data=[]
    with open(basedir+'/res/product_id') as f:
        for line in f:
            data.append(line.strip())
    print "load %d products" %len(data)
    return data

product_ids = get_products()

class AnccnetSpider(scrapy.Spider):
    name = "anccnet"
    allowed_domains = ["anccnet.com"]
    start_urls = (
        'http://search.anccnet.com/',
    )

    def parse(self, response):
        for idstr in product_ids:
            #http://search.anccnet.com/searchResult2.aspx?keyword=06922292300139
            yield scrapy.Request("http://search.anccnet.com/searchResult2.aspx?keyword="+idstr, self.parse_step2)
            #print idstr
        pass

    def parse_step2(self, response):
        #print response.text
        item = ProductinfoItem()
        try:        
            item['issn'] = response.xpath('//dl[@class="p-info"]/dd/a/text()').extract()[0]
            item['detail'] = response.xpath('//dl[@class="p-info"]/dd/a/@href').extract()[0]

            info = response.xpath('//dl[@class="p-info"]/dd/text()').extract()
            item['name'] = info[1]
            item['productType'] = info[2]
            item['desc'] = info[3]

            info = response.xpath('//dl[@class="p-supplier"]/dd/text()').extract()
            item['factory'] = response.xpath('//dl[@class="p-supplier"]/dd/a/text()').extract()[0]
            item['brand'] = info[0]

        except Exception, e:
            print response.url
            traceback.print_exc()
            return

        yield item


        #target = response.xpath('//dl[@class="p-info"]/dd/a/@href').extract()[0]
        #print target
        #yield scrapy.Request(target, self.parse_step3)

    def parse_step3(self, response):
        print response.text
        item = ProductinfoItem()
        try:
            item['isbnImg'] = response.css('#txt_gtin > img::attr("src")').extract()
            item['name'] = response.css('#Att_Sys_zh-cn_141_G/text()').extract()
            item['nameEn'] = response.css('#Att_Sys_en-us_141_G/text()').extract()
            item['unspcs'] = response.css('#Att_Sys_zh-cn_22_G/text()').extract()
            item['brand'] = response.css('#Att_Sys_zh-cn_304_G/text()').extract()
            item['modelType'] = response.css('#Att_Sys_zh-cn_332_G/text()').extract()
            item['width'] = response.css('#Att_Sys_zh-cn_101_G/text()').extract() + response.css('#Att_Sys_zh-cn_104_G/text()').extract()
            item['height'] = response.css('#Att_Sys_zh-cn_106_G/text()').extract() + response.css('#Att_Sys_zh-cn_326_G/text()').extract()
            item['depth'] = response.css('#Att_Sys_zh-cn_118_G/text()').extract()+ response.css('#Att_Sys_zh-cn_331_G/text()').extract()
            item['productCty'] = response.css('#Att_Sys_zh-cn_74_G/text()').extract()
            item['combileCty'] = response.css('#Att_Sys_zh-cn_171_G/text()').extract()
            item['productType'] = response.css('#Att_Sys_zh-cn_181_G/text()').extract()
           
        except Exception, e:
            print e
            return

        yield item
